package com.wondertek.onvif.dto;

import com.wondertek.onvif.entity.OnvifDevice;
import lombok.Data;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.ArrayList;

/**
 * 批量添加设备响应
 * 
 * @author wondertek
 * @version 1.0.0
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class BatchAddDevicesResponse {
    
    /**
     * 成功添加的设备数量
     */
    private int successCount;
    
    /**
     * 添加失败的设备数量
     */
    private int failureCount;
    
    /**
     * 总设备数量
     */
    private int totalCount;
    
    /**
     * 成功添加的设备列表
     */
    private List<OnvifDevice> successDevices = new ArrayList<>();
    
    /**
     * 添加失败的设备信息
     */
    private List<FailedDevice> failedDevices = new ArrayList<>();
    
    /**
     * 添加失败的设备信息
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class FailedDevice {
        /**
         * 设备IP地址
         */
        private String ipAddress;
        
        /**
         * 设备端口
         */
        private Integer port;
        
        /**
         * 失败原因
         */
        private String errorMessage;
        
        /**
         * 原始请求数据
         */
        private AddDeviceByIpRequest originalRequest;
    }
    
    /**
     * 添加成功的设备
     */
    public void addSuccessDevice(OnvifDevice device) {
        this.successDevices.add(device);
        this.successCount++;
    }
    
    /**
     * 添加失败的设备
     */
    public void addFailedDevice(String ipAddress, Integer port, String errorMessage, AddDeviceByIpRequest originalRequest) {
        this.failedDevices.add(new FailedDevice(ipAddress, port, errorMessage, originalRequest));
        this.failureCount++;
    }
    
    /**
     * 设置总数量
     */
    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }
    
    /**
     * 获取成功率
     */
    public double getSuccessRate() {
        if (totalCount == 0) {
            return 0.0;
        }
        return (double) successCount / totalCount * 100;
    }
}